package com.axend.aerosense.common.ui;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.PopupWindow;

import androidx.annotation.LayoutRes;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;

import java.lang.ref.WeakReference;
import java.lang.reflect.Field;

public abstract class BasePopupWindow<B extends ViewDataBinding>{

    protected B binding;
    protected PopupWindow mInstance;
    protected WeakReference<Activity> weakActivity;
    //布局
    protected int layoutRes;
    //父布局
    protected View parentView;
    //是否显示遮罩
    protected boolean showBackgroundMask;

    public BasePopupWindow(Activity activity, @LayoutRes int layoutRes) {
        this.weakActivity = new WeakReference<>(activity);
        this.layoutRes = layoutRes;
        this.binding = getLayout();
        initPopupWindow();
    }

    private B getLayout() {
        return DataBindingUtil.inflate(
                LayoutInflater.from(getActivity().getApplicationContext()),
                this.layoutRes,
                null,
                false);
    }

    private void initPopupWindow() {
        if (null == mInstance) {
            mInstance = new PopupWindow(
                    binding.getRoot(),
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    true);
            mInstance.setTouchable(true);
            mInstance.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            fitPopupWindowOverStatusBar(mInstance, true);
            mInstance.setOnDismissListener(() -> {
                //弹窗消失，清除背景
                setBackgroundAlpha(1.0f, false);
            });
            initView();
        }
    }

    /** 初始化视图与事件 */
    protected abstract void initView();

    protected void setBackgroundAlpha(float alpha, boolean isAdd){
        Window window = getActivity().getWindow();
        WindowManager.LayoutParams lp= window.getAttributes();
        //遮罩透明度
        lp.alpha = alpha;
        if (isAdd){
            window.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        }else {
            window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        }
        window.setAttributes(lp);
    }

    private void fitPopupWindowOverStatusBar(PopupWindow mPopupWindow, boolean needFullScreen) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            try {
                Field mLayoutInScreen = PopupWindow.class.getDeclaredField("mLayoutInScreen");
                mLayoutInScreen.setAccessible(needFullScreen);
                mLayoutInScreen.set(mPopupWindow, needFullScreen);
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
    }

    protected Activity getActivity(){
        if (null == weakActivity.get()) {
            throw new IllegalArgumentException("BasePopupWindow Argument is Null!");
        }
        return weakActivity.get();
    }




    public void showAsDropDown(View anchor) {
        showAsDropDown(anchor, 0, 0);
    }

    public void showAsDropDown(View anchor, int xOffset, int yOffset) {
        setBackgroundAlpha(showBackgroundMask ? 0.4f : 1.0f, true);
        mInstance.showAsDropDown(anchor, xOffset, yOffset);
    }

    public void showPopWindow(int gravity){
        showAtLocation(gravity, 0, 0);
    }

    //显示位置
    public void showAtLocation(int gravity, int x, int y) {
        setBackgroundAlpha(showBackgroundMask ? 0.4f : 1.0f, true);
        mInstance.showAtLocation(parentView, gravity, x, y);
    }

    //隐藏窗口
    public void dismiss(){
        mInstance.dismiss();
    }

    public BasePopupWindow setParentView(View parentView) {
        this.parentView = parentView;
        return this;
    }

    /** 是否显示遮罩 */
    public BasePopupWindow setShowBackgroundMask(boolean showBackgroundMask) {
        this.showBackgroundMask = showBackgroundMask;
        return this;
    }

    public int getWidth() {
        return mInstance.getWidth();
    }

    public int getHeight() {
        return mInstance.getHeight();
    }

    public View getParentView() {
        return parentView;
    }
}
